package com.wanmait.baohan.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.mapper.BookingCarApplicationMapper;
import com.wanmait.baohan.mapper.CarApprovalMapper;
import com.wanmait.baohan.pojo.BookingCarApplication;
import com.wanmait.baohan.pojo.CarApproval;
import com.wanmait.baohan.pojo.Staff;
import com.wanmait.baohan.service.BookingCarService;
import com.wanmait.baohan.util.CreatJobNumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2020/12/16 16:23
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BookingCarServiceImpl implements BookingCarService {

    @Resource
    private BookingCarApplicationMapper bookingCarApplicationMapper;
    @Resource
    private CarApprovalMapper carApprovalMapper;
    @Resource
    private HttpServletRequest request;

    /**
     * hxn
     * @param object
     * 提交派车申请
     */
    @Override
    public void insert(BookingCarApplication object) {
        object.setAddTime(new Date());
        object.setState(1);
        object.setNumbering("car" + CreatJobNumberUtil.creatJobNumber((Integer)request.getServletContext().getAttribute("carApplicationNumbering")));
        bookingCarApplicationMapper.insert(object);
    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(BookingCarApplication object) {

    }

    @Override
    public BookingCarApplication findById(Integer id) {
        return bookingCarApplicationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BookingCarApplication> findAll() {
        return null;
    }

    @Override
    public List<BookingCarApplication> findAll(BookingCarApplication object) {
        return null;
    }

    @Override
    public PageInfo<BookingCarApplication> findPage(int pageNum) {
        return null;
    }

    @Override
    public PageInfo<BookingCarApplication> findPage() {
        return null;
    }

    @Override
    public PageInfo<BookingCarApplication> findPage(int pageNum, BookingCarApplication object) {
        return null;
    }

    @Override
    public PageInfo<BookingCarApplication> findPage(BookingCarApplication object) {
        return null;
    }


    @Override
    public PageInfo<BookingCarApplication> selectMyCarApplications(Integer page, Integer limit, BookingCarApplication bookingCarApplication) {
        PageHelper.startPage(page, limit);
        List<BookingCarApplication> bookingCarApplications = bookingCarApplicationMapper.selectByStaffId(bookingCarApplication);
        PageInfo<BookingCarApplication> pageInfo = new PageInfo<>(bookingCarApplications, 7);
        return pageInfo;
    }

    @Override
    public PageInfo<BookingCarApplication> selectMyCarApplicationsList(Integer page, Integer limit, BookingCarApplication bookingCarApplication) {
        PageHelper.startPage(page, limit);
        List<BookingCarApplication> bookingCarApplications = bookingCarApplicationMapper.selectByState(bookingCarApplication);
        PageInfo<BookingCarApplication> pageInfo = new PageInfo<>(bookingCarApplications, 7);
        return pageInfo;
    }

    @Override
    public PageInfo<BookingCarApplication> selectAllCarApplicationsList(Integer page, Integer limit, BookingCarApplication bookingCarApplication) {
        PageHelper.startPage(page, limit);
        List<BookingCarApplication> bookingCarApplications = bookingCarApplicationMapper.selectAll(bookingCarApplication);
        PageInfo<BookingCarApplication> pageInfo = new PageInfo<>(bookingCarApplications, 7);
        return pageInfo;
    }

    @Override
    public BookingCarApplication findByIdCar(BookingCarApplication bookingCarApplication) {
        return null;//bookingCarApplicationMapper.selectByCar(bookingCarApplication);
    }

    @Override
    public void submitApproval(CarApproval carApproval) {
        Staff user = (Staff)request.getSession().getAttribute("user");
        carApproval.setStaff(user);
        carApproval.setApprovalTime(new Date());
        carApprovalMapper.insert(carApproval);
        bookingCarApplicationMapper.updateState(carApproval.getBookingCarApplicationId(), carApproval.getState());
    }
}
